layui.define('laydate', function (exports) {
        let $ = layui.$;
        let laydate = layui.laydate;
        let myDate = new Date();
        //api网关地址
        let GATEWAY_API_BASE = ZMN_CONFIG.BASE_URL.GATEWAY_API_BASE;

        let STAFF_DAY_LIMIT_URL = GATEWAY_API_BASE + "/mcc/api/StaffPermitRemoteService/listStaffIdAndDayLimitByStaffIdList";

        let zmnDate = {
            options: {
                staffId: null,//员工id
                defaultDayLimit: 365, //默认查询天数
                type: 'default', //  default 选一个时间如:2021/07/01  range 选一个时间段(2021/07/01 - 2021/07/17)
                dayLimit: null,  //指定选择范围
                rangeDays: 365,  //在选择范围内最多可选的天数(type=range时有效)
                formatStr: "yyyy/MM/dd",
                dateType: "datetime", //目前只支持日期 date 和日期时间选择器 datetime
                isInit: false, //初始默认值
                defaultValue: null, //初始默认值
                max: null, //初始默认值
                min: null //初始默认值
            },
            /**
             * 日期选择
             * @param inputId 输入框ID
             * @param options 参数对象
             */
            setZmnDate: function (inputId, options) {
                let config = $.extend(true, {}, this.options, options);
                let days = config.defaultDayLimit;
                let staffId = config.staffId;
                let dayLimit = config.dayLimit;
                let formatStr = zmnUtil.isEmptyNull(config.formatStr) ? "yyyy/MM/dd" : config.formatStr;
                let type = zmnUtil.isEmptyNull(config.type) ? 'default' : config.type;
                let dateType = (config.dateType === "datetime" || config.dateType === "date") ? config.dateType : "datetime";
                let isInit = config.isInit;
                let max = config.max;
                let min = config.min;
                let defaultValue = config.defaultValue;
                if (defaultValue == null||zmnUtil.isObject(defaultValue)) {
                    isInit = false;
                }
                if (zmnUtil.isEmptyNull(max)) {
                    max = null
                }
                if (zmnUtil.isEmptyNull(min)) {
                    min = null
                }

                if (staffId == null) {
                    console.log("staffId不能为空");
                    return false;
                }
                let arr = [staffId];
                $.ajax({
                    async: false,
                    type: "POST", url: STAFF_DAY_LIMIT_URL,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify({"body": {"staffIdList": arr}}),
                    success: function (result) {
                        if (result.status === 200) {
                            if (!zmnUtil.isNull(result.data) && result.data.length > 0) {
                                let arr = result.data[0];
                                days = arr.dayLimit;
                            }
                            if (!zmnUtil.isEmptyNull(dayLimit) && dayLimit < days && dayLimit > 0 && min == null) {
                                days = dayLimit;
                            }
                        }
                        executeLayDate(inputId, type, days, formatStr, dateType, isInit, defaultValue, max, min,config.rangeDays);
                    }
                });
            },
        };

        /**
         * 渲染 时间组织弹框
         * @param selectId
         * @param type 弹出类型 default 选一个时间如:2021/07/01  range 选一个时间段(2021/07/01 - 2021/07/17)
         * @param days 可选天数
         * @param formatStr 格式化
         * @param dateType 时间或日期选择
         * @param isInit 是否设置默认值
         * @param defaultValue 初始默认值
         * @param max 指定范围最大值
         * @param min 指定范围最小值
         * @param rangeDays 在指定选择范围内最多可选的天数
         */
        function executeLayDate(selectId, type, days, formatStr, dateType, isInit, defaultValue, max, min,rangeDays) {

            if (max != null) {
                if (zmnUtil.isInteger(max)) {
                    max = (max > 0) ? 0 : max;
                    let today = new Date(myDate.toLocaleDateString() + ' 23:59:59');
                    today.setDate(today.getDate() + max);
                    max = today.toLocaleDateString() + ' 23:59:59';
                    while (new RegExp("/").test(max)) {
                        max = max.replace('/', '-');
                    }
                } else {
                    let maxDate = new Date(max);
                    let today = new Date(myDate.toLocaleDateString() + ' 23:59:59');
                    if (maxDate > today) {
                        max = null;
                    }
                }
            }
            if (min != null) {
                if (zmnUtil.isInteger(min)) {
                    if (min < 0 && (min < (-days))) {
                        min = (-days);
                    }
                }
                if (zmnUtil.isInteger(min)) {
                    min = (min < 0) ? min : 0;
                    let today = new Date(myDate.toLocaleDateString() + ' 23:59:59');
                    min = min + 1;
                    today.setDate(today.getDate() + min);
                    min = today.toLocaleDateString() + ' 00:00:00';
                    while (new RegExp("/").test(min)) {
                        min = min.replace('/', '-');
                    }
                } else {
                    //最小值
                    let minDate = new Date(min);
                    let returnDate = new Date(myDate.toLocaleDateString() + ' 23:59:59');
                    //今天减去范围长度
                    returnDate.setDate(returnDate.getDate() - (days - 1));
                    if (minDate < returnDate) {
                        min = null;
                    }
                }
            }
            if (type === 'default') {
                if (isInit) {
                    let today = new Date(myDate.toLocaleDateString() + ' 23:59:59');
                    let defaultDate = new Date(defaultValue);
                    if (defaultDate > today) {
                        isInit = false;
                    }
                    //今天减去范围长度
                    today.setDate(today.getDate() - (days - 1));
                    if (defaultDate < today) {
                        isInit = false;
                    }
                }
                days = days - 1;
                laydate.render({
                    elem: "#" + selectId
                    , min: (min == null) ? (-days) : min
                    , max: (max == null) ? (myDate.toLocaleDateString() + ' 23:59:59') : max
                    , type: dateType
                    , format: formatStr
                    , value: defaultValue
                    , isInitValue: isInit
                });
            }
            if (type === "range") {
                if (isInit) {
                    let arr = defaultValue.split(" - ");
                    if (arr.length !== 2) {
                        isInit = false;
                    } else {
                        let today = new Date(myDate.toLocaleDateString() + ' 23:59:59');
                        let defaultDateBegin = new Date(arr[0]);
                        let defaultDateEnd = new Date(arr[1]);
                        if (defaultDateEnd > today) {
                            isInit = false;
                        }
                        //今天减去范围长度
                        today.setDate(today.getDate() - days);
                        if (defaultDateBegin < today) {
                            isInit = false;
                        }
                    }
                }
                days = days - 1;
              let rangeDay=laydate.render({
                    elem: "#" + selectId
                    , min: (min == null) ? (-days) : min
                    , max: (max == null) ? (myDate.toLocaleDateString() + ' 23:59:59') : max
                    , type: dateType
                    , range: '-'
                    , format: formatStr
                    , value: defaultValue
                    , isInitValue: isInit
                    , change:function(value, date, endDate){
                        let d = value.split('-');
                        let s = new Date(d[0]);
                        let e = new Date(d[1]);
                        //计算两个时间间隔天数
                        let differ=(e-s)/(1000*60*60*24);
                        if(differ>=rangeDays){
                            rangeDay.hint('最多选择'+rangeDays+'天');
                            setTimeout(function(){
                                $(".laydate-btns-confirm").addClass("laydate-disabled")
                            }, 100);
                        }
                    }
                });
            }
        }

        //对外暴露的接口
        exports('zmnDate', $.extend(true, {}, zmnDate));
    }
);